package cn.dvptech.remotecontrol.screenmirror.controller;

import android.media.AudioRecord;
import android.media.projection.MediaProjection;
import android.util.Log;

import cn.dvptech.remotecontrol.screenmirror.audio.AudioProcessor;
import cn.dvptech.remotecontrol.screenmirror.audio.AudioUtils;
import cn.dvptech.remotecontrol.screenmirror.audio.OnAudioEncodeListener;
import cn.dvptech.remotecontrol.screenmirror.configuration.AudioConfiguration;
import timber.log.Timber;

/**
 *    author  : ulongx
 *    website : www.dvptech.cn
 *    date    : 2024/7/28
 *    desc    :
 */
public class NormalAudioController implements IAudioController {
    private OnAudioEncodeListener mListener;
    private AudioRecord mAudioRecord;
    private AudioProcessor mAudioProcessor;
    private boolean mMute;
    private AudioConfiguration mAudioConfiguration;

    public NormalAudioController() {
        mAudioConfiguration = AudioConfiguration.createDefault();
    }

    public void setAudioConfiguration(AudioConfiguration audioConfiguration) {
        mAudioConfiguration = audioConfiguration;
    }

    public void setAudioEncodeListener(OnAudioEncodeListener listener) {
        mListener = listener;
    }

    public void start(MediaProjection mediaProjection) {
        Timber.d( "Audio Recording start");
        mAudioRecord = AudioUtils.getAudioRecord(mAudioConfiguration, mediaProjection);
        try {
            mAudioRecord.startRecording();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mAudioProcessor = new AudioProcessor(mAudioRecord, mAudioConfiguration);
        mAudioProcessor.setAudioHEncodeListener(mListener);
        mAudioProcessor.start();
        mAudioProcessor.setMute(mMute);
    }

    public void stop() {
        Timber.d( "Audio Recording stop");
        if(mAudioProcessor != null) {
            mAudioProcessor.stopEncode();
        }
        if(mAudioRecord != null) {
            try {
                mAudioRecord.stop();
                mAudioRecord.release();
                mAudioRecord = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void pause() {
        Timber.d( "Audio Recording pause");
        try {
            if (mAudioRecord != null) {
                mAudioRecord.stop();
            }
            if (mAudioProcessor != null) {
                mAudioProcessor.pauseEncode(true);
            }
        }catch (Exception e){
            Timber.tag("NormalAudioController").e(e);
        }
    }

    public void resume() {
        Timber.d("Audio Recording resume");
        if(mAudioRecord != null) {
            mAudioRecord.startRecording();
        }
        if (mAudioProcessor != null) {
            mAudioProcessor.pauseEncode(false);
        }
    }

    public void mute(boolean mute) {
        Timber.d("Audio Recording mute: %s", mute);
        mMute = mute;
        if(mAudioProcessor != null) {
            mAudioProcessor.setMute(mMute);
        }
    }

    @Override
    public int getSessionId() {
        if(mAudioRecord != null) {
            return mAudioRecord.getAudioSessionId();
        } else {
            return -1;
        }
    }
}
